import DateFormatConfig from "../../config/date-format-config"
import CommonService from "../../services/common-service"
import DynamicElementDataFactory from "../dynamic-element-data-factory"

// shared/dynamic-elements/table-element/table-element.ts

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    // json数据
    jsonData: Object,
    // 表头数据
    tableHeaderData: [],
    // 表格数据
    tableData: [],
    // 频率字典
    indexFrequencyList: [],
    // 频率数据
    frequencies: [],
    // 当前选择
    currentIndex: '',
    stripe: true,
    border: false,
    outBorder: true,
    msg: '暂无数据'
  },

  observers: {
    'item': function () {
      this.getData(this.properties.item);
    }
  },

  /**
 * 小程序组件生命周期
 */
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.getData(this.properties.item)
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取频率数据字典
     */
    getIndexFrequencyList: async function () {
      return await DynamicElementDataFactory.queryIndexFrequencyList1();
    },
    /**
     * data 数据赋值
     */
    getData: async function (item: any) {
      const frequencyResult = await this.getIndexFrequencyList()
      const frequencies = this.parseFrequencies(item.config.frequencies, frequencyResult.data)
      const tableDataResult = await DynamicElementDataFactory.queryIndexMultiPrice1(item, frequencies[0].FREQUENCY_CODE)

      this.setData({
        jsonData: item as any,
        tableHeaderData: this.parseTableHeaderData(item.config.headers),
        tableData: this.parseTbaleData(tableDataResult.data, item.config.headers),
        frequencies: frequencies,
        currentIndex: frequencies[0].FREQUENCY_CODE,
      })
    },

    /**
     * 解析拼接表格头部数据和配置
     */
    parseTableHeaderData: function (tableHeaderData: any) {
      return tableHeaderData.map((item: any) => {

        return {
          prop: item.PROP_NAME,
          width: 200,
          label: item.PROP_CAPTION,
          propType: item.PROP_TYP,
          propFlag: item.PROP_FLAG,
          // color: '#55C355'
        }
      })
    },
    /**
     * 解析表格数据
     * @param tableData 
     * @param tableHeaderData 
     */
    parseTbaleData: function (tableData: any, tableHeaderData: any) {
      return tableData.map((item: any) => {
        let color = ''

        for (let it in tableHeaderData) {
          // 更改Flag颜色
          if (tableHeaderData[it].PROP_FLAG == 1) {
            if (parseFloat(item[tableHeaderData[it].PROP_NAME]) < 0) {
              color = 'green';
            } else if (parseFloat(item[tableHeaderData[it].PROP_NAME]) > 0) {
              color = 'red'
            }
          }
          // 数据类型转换
          if (tableHeaderData[it].PROP_TYP == 'DateTime') {
            item[tableHeaderData[it].PROP_NAME] = CommonService.formatDate(new Date(item[tableHeaderData[it].PROP_NAME]), DateFormatConfig.shortDateFormat)
          }
        }

        item.color = color
        return item
      })
    },
    /**
     * 解析频率数据
     */
    parseFrequencies: function (frequencies: any, indexFrequencyList: any) {
      return indexFrequencyList.filter((item: any) => {
        return frequencies.indexOf(item.FREQUENCY_CODE) != -1;
      })
    },
    /**
     * 点击频率
     */
    onClickFrequency: async function (e: any) {
      const tableDataResult = await DynamicElementDataFactory.queryIndexMultiPrice1(this.data.jsonData as any, e.currentTarget.dataset.frequencycode)

      this.setData({
        currentIndex:  e.currentTarget.dataset.frequencycode,
        tableData: this.parseTbaleData(tableDataResult.data, (this.data.jsonData as any).config.headers),
      })
    }
  }
})
